/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ctrl;

import model.ViewUsersBackningBean;
import entities.GuestUser;
import hotel.jpa.Database;
import hotel.jpa.IJpaCtrl;
import hotel.jpa.UserJpaCtrl;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Anton Karlsson
 */
@ManagedBean(name="viewUsersControllerBean")
@ApplicationScoped
public class ViewUsersControllerBean {
    
    protected List<ViewUsersBackningBean> users = new ArrayList<ViewUsersBackningBean>();
    protected GuestUser user;
    
    public List<ViewUsersBackningBean> getUsers(){
        
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hotel_pu");
        IJpaCtrl productCtrl = new UserJpaCtrl(null, emf);
      
        users = productCtrl.findEntities();

        return users;
    }
    
    public void setProducts(List<ViewUsersBackningBean> u){
        this.users = u;
    }

    public GuestUser getUser() {
        return user;
    }

    public void setUser(GuestUser user) {
        this.user = user;
    }
    
    public String del(String name){
        
        List<GuestUser> us = Database.getUserController().findEntities();
        
        GuestUser user = null;
        for(GuestUser u : us){
            if(u.getName().compareTo(name) == 0){
                user = u;
            }
        }
        Database.getUserController().destroy(user);
       
        return "/admin/edituser.xhtml";
    }
    
    public String saveUser(GuestUser u) {
        user = u;
        return "/admin/edituser_step2.xhtml";
    }
    
    public String edit(GuestUser u){
        
        Database.getUserController().edit(u);
        
        return "/admin/edituser.xhtml";
    }
}
